﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using BLL;
using Entities;
namespace Stationery.UC
{
    public partial class UC_ProductDetail : System.Web.UI.UserControl
    {
        BLL_Product bll_Product = new BLL_Product();
        public string str = "~/Image/banner_hotline.png";
        protected void Page_Load(object sender, EventArgs e)
        {
            //string strID = HttpUtility.ParseQueryString(uri.Query).Get("id");
            if (!IsPostBack)
            {
                if (Request.Params["id"] != null)
                {
                    string strID = Request.Params["id"].ToString();

                    ASPxDataViewProduct.DataSource = bll_Product.Get_Product_ID_MenuURL(Convert.ToInt32(strID));
                    ASPxDataViewProduct.DataBind();
                    Set_Info();
                }
            }
        }
        protected string GetStatusStore(object pStatus)
        {
            try
            {
                if (pStatus.ToString() == "O")
                {
                    return "(hết hàng)";
                }
                else if (pStatus.ToString() == "C")
                {
                    return "(sắp có)";
                }
                else
                {
                    return "(còn hàng)";
                }
            }
            catch
            {
                return "(hết hàng)";
            }
        }
        protected string GetColorStatus(object pStatus)
        {
            try
            {
                if (pStatus.ToString() == "O")
                {
                    return "Red";
                }
                else
                {
                    return "Green";
                }
            }
            catch
            {
                return "Red";
            }
        }
        private void Set_Info()
        {
            try
            {
                string strID_CHILD = Request.Params["id_child"].ToString();
                Product product = bll_Product.Lst_Product(bll_Product.Get_Product(Convert.ToInt64(strID_CHILD)))[0];
                LabelName.Text = product.NAME;
                LabelPrice.Text = product.PRICE.ToString("### ### ###") + " VNĐ";
                LabelDescription.Text = product.DESCRIPTION;
                ImageButton1.ImageUrl = product.IMAGE_DIRECTORY;
                //LabelOrigin.Text = product.ORIGIN;
                LabelOtherName.Text = product.OTHER_NAME;
                if (product.WEIGHT > 0)
                {
                    LabelWeight.Text = (product.WEIGHT/1000).ToString()+ " kg";
                }
                //LabelSALE.Text = product.SALE.ToString() + " %";
                LabelStat.Text = product.STAT_STOCK_NAME;
                //LabelStorageTime.Text = product.STORAGE_TIME;
                AspLinkButtonContinue.CommandArgument = strID_CHILD;
                AspLinkButtonCart.CommandArgument = strID_CHILD;
                //popup image
                //ASPxImage1.ImageUrl = product.IMAGE_DIRECTORY;
                //ASPxPopupControl1.HeaderText = product.NAME;
                switch (product.STAT_STOCK)
                {
                    case "I":
                        AspLinkButtonCart.Visible = true;
                        AspLinkButtonContinue.Visible = true;
                        break;
                    case "O":
                        AspLinkButtonCart.Visible = false;
                        AspLinkButtonContinue.Visible = false;
                        break;
                    case "C":
                        AspLinkButtonCart.Visible = false;
                        AspLinkButtonContinue.Visible = false;
                        break;
                    default:
                        AspLinkButtonCart.Visible = false;
                        AspLinkButtonContinue.Visible = false;
                        break;
                }
            }
            catch 
            {
                Response.Redirect(UtilsURL.home);
            }
        }

        protected void ASPxButtonDetail_Click(object sender, EventArgs e)
        {
            string strID_PARENT = "";
            string strID = "";
            string strURL = "";
            strID = ((ASPxButton)sender).CommandArgument.ToString();
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(Convert.ToInt64(strID)))[0];
                strID_PARENT = product.ID_MENUURL.ToString();
            }
            catch
            {
                return;
            }
            strURL = String.Format("WebProduct.aspx?id={0}&id_child={1}", strID_PARENT, strID);
            Response.Redirect(strURL);
        }
        protected string GetLinkDetail(object pID)
        {
            string strID_PARENT = "";
            int intID;
            string strURL = "";
            intID = Convert.ToInt32(pID);
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(intID))[0];
                strID_PARENT = product.ID_MENUURL.ToString();
            }
            catch
            {
                return "";
            }
            strURL = String.Format("WebProduct.aspx?id={0}&id_child={1}", strID_PARENT, intID);
            return strURL;
        }
        protected void ASPxButtonCart_Click(object sender, EventArgs e)
        {
            string strID = "";
            strID = (sender as LinkButton).CommandArgument.ToString();
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(Convert.ToInt64(strID)))[0];
            }
            catch
            {
                return;
            }
            //
            Order order = new Order();
            order.ID = 0;
            order.ID_PRODUCT = product.ID;
            order.PRICE = product.PRICE;
            order.QUANTITY = 1;
            order.TAX = 0;
            UpdatePriceValue(order);
            order.TYPE_PAY = "0";
            order.PRODUCT_DETAIL = product;
            //
            List<Order> lstOrder = new List<Order>();
            if (Session[Utils.SESSION_CART] != null)
            {
                lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                for (int i = 0; i < lstOrder.Count; i++)
                {
                    if (lstOrder[i].ID_PRODUCT == order.ID_PRODUCT)//da ton tai san pham trong gio hang roi thi cong don
                    {
                        lstOrder[i].QUANTITY += 1;
                        UpdatePriceValue(lstOrder[i]);
                        break;
                    }
                    if (i == lstOrder.Count - 1)//phan tu cuoi cung ma chua ton tai thi them moi
                    {
                        order.ID = i + 1;
                        lstOrder.Add(order);
                        break;
                    }
                }
            }
            else
            {
                lstOrder.Add(order);
            }
            Session[Utils.SESSION_CART] = lstOrder;
            Response.Redirect(UtilsURL.order);
        }
        private void UpdatePriceValue(Order pOrder)
        {
            pOrder.VALUE = pOrder.PRICE * pOrder.QUANTITY;
            pOrder.TOTAL = pOrder.VALUE + pOrder.VALUE * pOrder.TAX;
        }

        protected void ASPxButtonFastCart_Click(object sender, EventArgs e)
        {
            string strID = "";
            strID = (sender as LinkButton).CommandArgument.ToString();
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(Convert.ToInt64(strID)))[0];
            }
            catch
            {
                return;
            }
            //
            Order order = new Order();
            order.ID = 0;
            order.ID_PRODUCT = product.ID;
            order.PRICE = product.PRICE;
            order.QUANTITY = 1;
            order.TAX = 0;
            UpdatePriceValue(order);
            order.TYPE_PAY = "0";
            order.PRODUCT_DETAIL = product;
            //
            List<Order> lstOrder = new List<Order>();
            if (Session[Utils.SESSION_CART] != null)
            {
                lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                for (int i = 0; i < lstOrder.Count; i++)
                {
                    if (lstOrder[i].ID_PRODUCT == order.ID_PRODUCT)//da ton tai san pham trong gio hang roi thi cong don
                    {
                        lstOrder[i].QUANTITY += 1;
                        UpdatePriceValue(lstOrder[i]);
                        break;
                    }
                    if (i == lstOrder.Count - 1)//phan tu cuoi cung ma chua ton tai thi them moi
                    {
                        order.ID = i + 1;
                        lstOrder.Add(order);
                        break;
                    }
                }
            }
            else
            {
                lstOrder.Add(order);
            }
            Session[Utils.SESSION_CART] = lstOrder;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Đã thêm vào giỏ hàng');location.href = location.href;", true);
        }
        //ImageUrl='<%# Eval("IMAGE_DIRECTORY") %>'
        protected bool GetVisibleStatus(object pStatus)
        {
            try
            {
                if (pStatus.ToString() == "O" || pStatus.ToString() == "C")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}